package com.atguigu.tingshu;

import com.atguigu.tingshu.common.constant.RedisConstant;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableScheduling;

@Slf4j
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableScheduling //开启定时任务
public class ServiceAlbumApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(ServiceAlbumApplication.class, args);
    }

    @Autowired
    private RedissonClient redissonClient;

    @PostConstruct
    public void initBloomFilter() {
        log.info("模拟初始化布隆过滤器开始...");
    }

    /**
     * springboot启动后自动执行一次该方法
     * 项目启动后自动初始化布隆过滤器
     */
    @Override
    public void run(String... args) throws Exception {
        //1.创建/获取布隆过滤器对象
        RBloomFilter<Long> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        //2.判断过滤过滤器是否存在
        boolean exists = bloomFilter.isExists();
        log.info("布隆过滤器是否存在：{}", exists);
        if (!exists) {
            boolean flag = bloomFilter.tryInit(10000L, 0.03);
            log.info("初始化布隆过滤器，完成结果：{}", flag);
        }
    }


}
